package com.sa07.ui.components;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 测试结果控件
 * 
 * @author Long.shangbo
 * 
 *         2014-2-17 下午9:14:14
 */
public class JtestResultInfoBar extends JPanel {

	private static final long serialVersionUID = -4363063657424924680L;

	private JLabel totalNumLbl = null;

	private JLabel successNumLbl = null;

	private JLabel faildNumLbl = null;

	private JLabel successPersentLbl = null;

	private int totalNum = 0;

	private int successNum = 0;

	private int failedNum = 0;
	
	private NumberFormat formatter = new DecimalFormat("#0.00");

	public JtestResultInfoBar() {
		setBorder(BorderFactory.createTitledBorder("测试结果"));
		setLayout(null);
		initComponents();
		initValues();
	}
	
	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
		initValues();
	}
	
	public int getTotalNum() {
		return totalNum;
	}
	
	public void addSuccessNum(int successNum) {
		this.successNum += successNum;
		initValues();
	}
	
	public int getSuccessNum() {
		return successNum;
	}
	
	public void setFailedNum(int failedNum) {
		this.failedNum += failedNum;
		initValues();
	}
	
	public int getFailedNum() {
		return failedNum;
	}
	
	private void initValues() {
		totalNumLbl.setText(String.format("测试数量：%d个", totalNum));
		successNumLbl.setText(String.format("通过数量：%d个", successNum));
		faildNumLbl.setText(String.format("未通过数量：%d个", failedNum));
		successPersentLbl.setText(String.format("测试通过率：%s", (totalNum == 0 ? 0.00 : formatter.format(successNum / totalNum)) + "%"));
	}

	private void initComponents() {
		int x = 25;
		int y = 28;
		int testResultHeight = 20;
		int margin = 10;

		totalNumLbl = new JLabel();
		int testAllNumWidth = 170;
		totalNumLbl.setBounds(x, y, testAllNumWidth, testResultHeight);
		add(totalNumLbl);

		x += testAllNumWidth;

		successNumLbl = new JLabel();
		int successNumWidth = 170;
		successNumLbl.setBounds(x + margin, y, successNumWidth,
				testResultHeight);
		add(successNumLbl);

		x += successNumWidth;

		faildNumLbl = new JLabel();
		int failedNumWidth = 200;
		faildNumLbl.setBounds(x + margin, y, failedNumWidth, testResultHeight);
		add(faildNumLbl);

		x += failedNumWidth;

		successPersentLbl = new JLabel("测试通过率：50%");
		int successPersentWidth = 200;
		successPersentLbl
				.setBounds(x, y, successPersentWidth, testResultHeight);
		add(successPersentLbl);
	}

	public static void main(String[] args) {
		NumberFormat formatter = new DecimalFormat("#0.00");
		double money = 100550000;
		System.out.println(formatter.format(money));;
	}
}
